package com.github.io.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.io.annotation.Api;
import com.github.io.entity.SbApiResultExplanationData;
import com.github.io.service.ISbApiResultExplanationDataService;
import com.github.io.utils.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2022/9/13 17:09
 * Description:
 */
@RestController
@Api(value = "Api返回参数控制器")
@RequestMapping("/apiResultExp")
public class ApiResultExpDataController {

    @Autowired
    private ISbApiResultExplanationDataService resultExplanationDataService;


    /**
     * 详情接口
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Object getApiInfo(@PathVariable("id") String id) {
        Assert.notNull(id, "id不能为空");
        Map<String, Object> result = new HashMap<>();
        try {
            SbApiResultExplanationData resultExplanationData = resultExplanationDataService.getResultExpInfo(id);
            if (!ObjectUtils.isEmpty(resultExplanationData)) {
                result.put("status", 200);
                result.put("msg", "获取成功");
                result.put("data", resultExplanationData);
            } else {
                result.put("status", 400);
                result.put("msg", "获取失败，数据id不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", 400);
            result.put("msg", "获取Api返回信息说明时异常");
        }
        return result;
    }


    /**
     * 接口响应字段说明 新增接口
     *
     * @param resultExplanationData
     * @return
     */
    @PostMapping("/saveResultExp")
    public Object saveResultExplanationData(@RequestBody SbApiResultExplanationData resultExplanationData) {
        Assert.notNull(resultExplanationData.getApiId(), "接口id不能为空");
        Map<String, Object> result = new HashMap<>();
        try {
            int count = resultExplanationDataService.saveResultExplanationData(resultExplanationData);
            if (count > 0) {
                result.put("status", 200);
                result.put("msg", "新增成功");
            } else {
                result.put("status", 400);
                result.put("msg", "新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", 400);
            result.put("msg", "新增时异常");
        }
        return result;
    }


    /**
     * 接口响应字段说明 修改接口
     *
     * @param resultExplanationData
     * @return
     */
    @PutMapping("/updateResultExp")
    public Object updateResultExp(@RequestBody SbApiResultExplanationData resultExplanationData) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = resultExplanationDataService.updateResultExp(resultExplanationData);
            if (count > 0) {
                result.put("status", 200);
                result.put("msg", "修改成功");
            } else {
                result.put("status", 400);
                result.put("msg", "修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", 400);
            result.put("msg", "修改时异常");
        }
        return result;
    }


    /**
     * 接口下所有响应参数 树形数据接口
     *
     * @param apiId
     * @return
     */
    @GetMapping("/getResultExp")
    public Object getResultExp(@RequestParam String apiId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<SbApiResultExplanationData> resultExplanationDataTree = resultExplanationDataService.selectTree(apiId);
            result.put("status", 200);
            result.put("msg", "修改成功");
            result.put("data", resultExplanationDataTree);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", 400);
            result.put("msg", "修改时异常");
        }
        return result;
    }

    
    /**
     * 详情接口
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Object delete(@PathVariable("id") String id) {
        Assert.notNull(id, "id不能为空");
        Map<String, Object> result = new HashMap<>();
        try {
            QueryWrapper<SbApiResultExplanationData> resultExplanationDataQueryWrapper = new QueryWrapper<>();
            resultExplanationDataQueryWrapper.eq("pid", id);
            List<SbApiResultExplanationData> resultExplanationDataList = resultExplanationDataService.list(resultExplanationDataQueryWrapper);
            if (!CollectionUtils.isEmpty(resultExplanationDataList)) {
                result.put("status", 400);
                result.put("msg", "删除失败，其下存在数据，无法删除");
            } else {
                boolean isDel = resultExplanationDataService.removeById(id);
                if (isDel) {
                    result.put("status", 200);
                    result.put("msg", "获取成功");
                } else {
                    result.put("status", 400);
                    result.put("msg", "删除失败，该数据不存在");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", 400);
            result.put("msg", "获取Api返回信息说明时异常");
        }
        return result;
    }
}
